<?php
namespace Admin\Controller;
use Admin\Controller\AdminBaseController;
use Common\Model\SellModel;
use Common\Model\UserModel;
class SellController extends AdminBaseController{
	public function sellLog()
	{
	    if ($_GET['username']) {
           $username=I('username'); 
           $user=$this->findUserByUserName($username);
	       $where['sell_id']=$user['id'];
	    }
        $list=SellModel::newSelf()->pagingList($where);
        $this->assign('list',$list);
        $this->display();
	} 
	public function revokeSell()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请正确操作');
        $sell_model=SellModel::newSelf();
        $sell_info=$sell_model->findOneById($id);
        
        $this->eqValidate($sell_info['status'],'0','此挂单无法撤销');
        

        $data['success_time']=time();
        $data['status']=-1;
        $res=$sell_model->where(['id'=>$id])->save($data);
        
        UserModel::newSelf()->incUserMondey($sell_info['sell_id'], $sell_info['num'], $sell_info['currency_id']);
        
        $this->resReturn($res,'撤单成功','系统繁忙请稍后再试');
    }
    public function giveUpBuySub()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请正确操作');
        $sell_model=SellModel::newSelf();
        $sell_info=$sell_model->findOneById($id);
    
        $this->eqValidate($sell_info['status'],'1','此挂单无法放弃');
    
        $data['buy_id']='';
        $data['buy_time']='';
        $data['status']='';
    
        $res=$sell_model->where(['id'=>$id])->save($data);
        $this->resReturn($res,'成功放弃此次交易','系统繁忙请稍后再试');
    
    }
    public function paySellSub()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请正确操作');
        $sell_model=SellModel::newSelf();
        $sell_info=$sell_model->findOneById($id);
    
        $this->eqValidate($sell_info['status'],'1','此挂单无法付款确认');
    
        $data['pay_time']=time();
        $data['status']=2;
    
        $res=$sell_model->where(['id'=>$id])->save($data);
        $this->resReturn($res,'付款确认成功','系统繁忙请稍后再试');
    
    }
    public function sureSellSub()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请正确操作');
        $sell_model=SellModel::newSelf();
        $sell_info=$sell_model->findOneById($id);
    
        $this->eqValidate($sell_info['status'],'2','此挂单无法收款');
    
        $data['success_time']=time();
        $data['status']=3;
    
        $res=$sell_model->where(['id'=>$id])->save($data);
        UserModel::newSelf()->incUserMondey($sell_info['buy_id'], $sell_info['num'], $sell_info['currency_id']);
        $this->resReturn($res,'交易成功','系统繁忙请稍后再试');
    
    }
}
